package me.rocky.service;

import me.rocky.domain.AuthUser;
import me.rocky.entity.SysUser;
import me.rocky.entity.SysUserExample;
import me.rocky.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Rocky
 * @version 1.0
 * @description
 * @email inaho00@foxmail.com
 * @createDate 2021/9/22 22:35
 * @log
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

	@Autowired
	private SysUserMapper sysUserMapper;

	@Override
	public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
		SysUserExample e = new SysUserExample();
		e.createCriteria().andUsernameEqualTo(s);
		List<SysUser> sysUsers = sysUserMapper.selectByExample(e);
		if (sysUsers.isEmpty()){
			throw new UsernameNotFoundException("没找到用户");
		}
		SysUser sysUser = sysUsers.get(0);
		return new AuthUser(sysUser);
	}
}
